import { Component, OnInit } from '@angular/core';
import { HttpService } from 'src/app/service/http.service';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { ToolService } from 'src/app/service/tool.service';
import { NavController } from '@ionic/angular';
import { PopwinService } from 'src/app/service/popwin.service';
import { EventService } from 'src/app/service/event.service';
import { ActivatedRoute, Params } from '@angular/router';
import { Storage } from '@ionic/storage';

@Component({
  selector: 'app-banklist',
  templateUrl: './banklist.page.html',
  styleUrls: ['./banklist.page.scss'],
})
export class BanklistPage implements OnInit {
  public listData: any[] = [];
  public isSelect=false;
  constructor(private http: HttpService, private navCtrl: NavController, private popWin: PopwinService,private eventS:EventService,private activatedRoute:ActivatedRoute,private store:Storage) { 
      this.activatedRoute.queryParams.subscribe((params:Params)=>{
        let tempDeal=JSON.parse(ToolService.paramAESDecrypt(params['p']));
          if(tempDeal['s']=='1'){
              this.isSelect=true;
          }
      });
  }

  ngOnInit() {
    this.showBankList();
    this.initEvent();
  }

  initEvent(){
      let that=this;
      this.eventS.subscribe("showBankList",function(){
         that.showBankList();
      },true);
  }

  showBankList() {
    let that = this;
    this.http.get(UrlService.USER_SHOWUSERBANK, { "callback": "JSONP_CALLBACK" }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let res = ToolService.aesDecodeToJson(temp);
        that.listData = res.bankinfo;
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }
  /**
   * 反回个人中心
   */
  backClick() {
    this.navCtrl.back();
    if(this.isSelect){
      this.eventS.publish("selectCard");
    }
  }
  /**
   * 添加银行卡
   */
  onAdd() {
    this.navCtrl.navigateForward("/bankadd");
  }

  onDeleteCard(event,cardID) {
    event.stopPropagation();
    event.preventDefault();
    let that = this;
    let obj = "{\"bankid\":\"" + cardID + "\"}";
    this.http.get(UrlService.USER_DELUSERBANK, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
          that.showBankList();
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
    });
  }
  /**
   * 选中银行卡
   */
  onSelect(item){
      if(this.isSelect){
        this.store.set("card",item);
        this.backClick();
      }
  }

  hideName(val:String){
    return "**"+val.substr(-1,1);
   }

}
